package com.wh.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import com.wh.model.SysMenu;
import com.legend.utils.ReturnResult;

public class GetServiceIns {
    static Logger log = Logger.getLogger(GetServiceIns.class);

    public static IBaseService newInstance(String className) {
        IBaseService bo = null;
        try {
            Object obj = Class.forName("com.wh.service.impl." + className + ".getInstance()");
            bo = (IBaseService) obj;

        } catch (ClassNotFoundException e) {
            log.error(e.getMessage());
            // e.printStackTrace();
        }
        return bo;
    }

    public static IBaseService getInstance(String className) {
        try {
            Class cls = Class.forName("com.wh.service.impl." + className);
            Object o = cls.getMethod("getInstance").invoke(null);
            IBaseService service = (IBaseService) o;
            return service;
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage());
            // e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String args[]) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException {
        //Object o=  Class.forName("com.wh.service.impl.SysMenuService").getMethod("getInstance").invoke(null);
log.debug("测试数据debug");
        log.info("测试数据info");

        log.trace("测试数据trace");
        log.error("测试错误数据");
        Class cls = Class.forName("com.wh.service.impl.SysMenuService");
        Object o = cls.getMethod("getInstance").invoke(null);
        IBaseService service = (IBaseService) o;
        ReturnResult result = service.selectById("1");
        System.out.printf(result.getMsg());
//        IBaseService bo = newInstance("SysMenuService");
//		SysMenu s = new SysMenu();
//		s.setId("25");
//		ReturnResult result = bo.insertByModel(s);
//		System.out.println(result.getMsg());
//
//		Class clazz = bo.getClass();
//		Method m = clazz.getDeclaredMethod("add",Object.class);
//		s.setId("8883883");
//		result = (ReturnResult) m.invoke(bo, s);
//		System.out.println(result.getMsg());
    }
}
